import { Injectable, EventEmitter } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'

@Injectable()
export class masterService {
    // 打开新建商品标签tab
    public createMarkList: EventEmitter<any> = new EventEmitter<any>();

    // 活动商品管理
    // 打开活动商品tab
    public openActivity: EventEmitter<any> = new EventEmitter<any>();
    // 关闭活动商品tab
    public closeActivity: EventEmitter<any> = new EventEmitter<any>();
    // 活动商品列表刷新
    public ActivityReload: EventEmitter<any> = new EventEmitter<any>();
    // 一键下单列表刷新
    public OneClickReload: EventEmitter<any> = new EventEmitter<any>();

    // 一键下单管理
    // 打开一键下单详情tab
    public openClickOrder: EventEmitter<any> = new EventEmitter<any>();
    // 关闭活动商品tab
    public closeClickOrder: EventEmitter<any> = new EventEmitter<any>();

    // 引导下载活动管理
    // 引导下载活动列表刷新
    public LeadDownloadReload: EventEmitter<any> = new EventEmitter<any>();
    // 打开引导下载活动详情tab
    public openLeadDownload: EventEmitter<any> = new EventEmitter<any>();
    // 关闭活动商品tab
    public closeLeadDownload: EventEmitter<any> = new EventEmitter<any>();
    //新建卡券生成批次
    public openCardBatchinfo: EventEmitter<any> = new EventEmitter<any>();
    // 打开卡券模板
    public openCardTemplate: EventEmitter<any> = new EventEmitter<any>();
    // 关闭卡券模板
    public closeCardTemplate: EventEmitter<any> = new EventEmitter<any>();
    // 卡券模板列表刷新
    public CardTemplateReload: EventEmitter<any> = new EventEmitter<any>();

    // 保存tab状态
    public saveTabs: EventEmitter<any> = new EventEmitter<any>();

    public Authorization = 'Bearer ' + localStorage.access_token;
    constructor(private _http: Http) { }
    // 获取交互的数据
    public getData(Url, UrlSearchParams?, header?): any {
        let headers = new Headers(header);
        if (localStorage.access_token) {
            headers.append('Authorization', 'Bearer ' + localStorage.access_token);
        }
        let options = new RequestOptions({ headers: headers });
        if (UrlSearchParams) {
            Url += '?';
            for (let prop in UrlSearchParams) {
                Url += prop + '=' + UrlSearchParams[prop] + '&';
            }
            Url = Url.substring(0, Url.length - 1);
        }

        return this._http.get(Url, options)
            .pipe(map(this.toJson))
    }

    public postData(Url, UrlSearchParams?, header?): any {
        let headers = new Headers(header);
        if (localStorage.access_token) {
            headers.append('Authorization', 'Bearer ' + localStorage.access_token);
        }
        let options = new RequestOptions({ headers: headers });
        return this._http.post(Url, UrlSearchParams, options)
            .pipe(map(this.toJson))
    }

    public postLoginData(Url, UrlSearchParams?, header?): any {
        let headers = new Headers(header);
        if (localStorage.access_token) {
            headers.append('Authorization', 'Bearer ' + localStorage.access_token);
        }
        let options = new RequestOptions({ headers: headers });
        return this._http.post(Url, UrlSearchParams, options)
    }

    // 转换得到的数据格式
    private toJson(res: Response) {
        let result = res.json();
        return result || [];
    }

    // 打印出错的信息
    private handleError(error: Response) {
        return Observable.throw(error || '服务器出错');
    }
}
